package com.ai.controller.security;


import com.ai.annotation.EnterPathParamLog;
import com.ai.common.CommonResponse;
import com.ai.entity.security.SysRole;
import com.ai.entity.security.page.SysRolePage;
import com.ai.model.param.BindingPermissionParam;
import com.ai.model.param.SysRoleParam;
import com.ai.service.security.ISysRoleService;
import com.ai.common.vaild.AddGroup;
import com.ai.common.vaild.UpdateGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户角色接口
 *
 * @author chen
 * @since 2023-04-04 19:46:07
 */
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;


    @ApiOperation("分页查询")
    @PostMapping("list")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public IPage<SysRole> list(@RequestBody SysRolePage page) {

        // 根据创建时间排序
        QueryWrapper<SysRole> queryWrapper = page.getQuery(new QueryWrapper<>());
        IPage<SysRole> iPage = sysRoleService.page(page, queryWrapper);
        return iPage;
    }


    @ApiOperation("查询指定角色")
    @PostMapping("selectById/{id}")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public CommonResponse<Object> selectById(@PathVariable(name = "id") Integer id) {
        SysRole byId = sysRoleService.getById(id);
        return CommonResponse.ok("查询成功").setData(byId);
    }

    //新增
    @ApiOperation("新增角色")
    @PostMapping("add")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public CommonResponse<String> add(@RequestBody @Validated({AddGroup.class}) SysRoleParam sysRoleParam) {
        return sysRoleService.add(sysRoleParam);
    }

    @ApiOperation("删除角色")
    @PostMapping("delete/{id}")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public CommonResponse<String> delete(@PathVariable Integer id) {
        return  sysRoleService.removeRoleById(id);
    }

    @ApiOperation("更新角色")
    @PostMapping("update")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public CommonResponse<String> update(@RequestBody @Validated({UpdateGroup.class}) SysRoleParam sysRoleParam) {

        return sysRoleService.updateByParam(sysRoleParam);

    }


    @ApiOperation("角色绑定角色信息")
    @PostMapping("bindingPermission")
    @EnterPathParamLog
    @PreAuthorize("hasAuthority('ROLE_MANAGE')")
    public CommonResponse<String> bindingPermission(@RequestBody BindingPermissionParam permissionParam) {

        return sysRoleService.bindingPermission(permissionParam);

    }

}

